package com.qeado.report.service.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qeado.report.dao.ExportProdInfoDao;
import com.qeado.report.model.ProdExport;
import com.qeado.report.service.ExportProdInfoService;

@Service
public class ExportProdInfoServiceImpl implements ExportProdInfoService {
	@Autowired
	private ExportProdInfoDao exportProdInfoDao;

	public List<ProdExport> prodInfoExportSearch(ProdExport prodExport) {
		return exportProdInfoDao.prodInfoExportSearch(prodExport);
	}

	@Override
	public List<ProdExport> searchBrandList() {
		return exportProdInfoDao.searchBrandList();
	}

	@Override
	public List<ProdExport> searchProdTypeList() {
		return exportProdInfoDao.searchProdTypeList();
	}

	public WritableWorkbook prodInfoToExcel(ProdExport prodExport, OutputStream out) {
		List<ProdExport> prodExportList = exportProdInfoDao.prodInfoExportSearch(prodExport);
		WritableWorkbook book = null;
		try {
			book = Workbook.createWorkbook(out);
			WritableSheet sheet = book.createSheet("商品信息", 0); // 创建Sheet
			//表头标题字体格式
			WritableFont headTitleFont = new WritableFont(WritableFont.createFont("宋体_GB2312"), 10, WritableFont.BOLD, false);
			//表头标题表格样式
			WritableCellFormat headTitleFormat = new WritableCellFormat(headTitleFont);
			headTitleFormat.setBackground(jxl.format.Colour.PALE_BLUE);
			headTitleFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
			headTitleFormat.setWrap(true);//自动换行
			headTitleFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			headTitleFormat.setAlignment(jxl.format.Alignment.CENTRE);
			//内容字体格式
			WritableFont contentFont = new WritableFont(WritableFont.createFont("宋体_GB2312"), 10, WritableFont.NO_BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
			//内容表格样式，居中对齐
			WritableCellFormat contentCenterFormat = new WritableCellFormat(contentFont);
			contentCenterFormat.setBackground(jxl.format.Colour.LIGHT_GREEN);
			contentCenterFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
			contentCenterFormat.setWrap(true);//自动换行
			contentCenterFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			contentCenterFormat.setAlignment(jxl.format.Alignment.CENTRE);
			//靠左对齐
			WritableCellFormat contentLeftFormat = new WritableCellFormat(contentFont);
			contentLeftFormat.setBackground(jxl.format.Colour.LIGHT_GREEN);
			contentLeftFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
			contentLeftFormat.setWrap(true);//自动换行
			contentLeftFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			contentLeftFormat.setAlignment(jxl.format.Alignment.LEFT);
			
			//靠右对齐
			WritableCellFormat contentRightFormat = new WritableCellFormat(contentFont);
			contentRightFormat.setBackground(jxl.format.Colour.LIGHT_GREEN);
			contentRightFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
			contentRightFormat.setWrap(true);//自动换行
			contentRightFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
			contentRightFormat.setAlignment(jxl.format.Alignment.RIGHT);
			
			sheet.addCell(new Label(0, 0, "信息类别", headTitleFormat)); // 标题格式的单元格
			sheet.addCell(new Label(1, 0, "商品名称", headTitleFormat));
			sheet.addCell(new Label(2, 0, "商品种类", headTitleFormat));
			sheet.addCell(new Label(3, 0, "价格", headTitleFormat));
			sheet.addCell(new Label(4, 0, "商品品牌", headTitleFormat));
			sheet.addCell(new Label(5, 0, "产品级别", headTitleFormat));
			sheet.addCell(new Label(6, 0, "是否热门产品", headTitleFormat));
			sheet.addCell(new Label(7, 0, "产品所有者", headTitleFormat));
			sheet.addCell(new Label(8, 0, "数量", headTitleFormat));
			sheet.addCell(new Label(9, 0, "单位", headTitleFormat));
			sheet.addCell(new Label(10, 0, "发布日期", headTitleFormat));
			sheet.addCell(new Label(11, 0, "截至日期", headTitleFormat));
			
			sheet.setColumnView(0, 10); // 设置列的宽度
			sheet.setColumnView(1, 20); 
			sheet.setColumnView(2, 10); 
			sheet.setColumnView(3, 12); 
			sheet.setColumnView(4, 12); 
			sheet.setColumnView(5, 12); 
			sheet.setColumnView(6, 15); 
			sheet.setColumnView(7, 13); 
			sheet.setColumnView(8, 10); 
			sheet.setColumnView(9, 10); 
			sheet.setColumnView(10, 12); 
			sheet.setColumnView(11, 12); 
			
			if(prodExportList != null) {
				for(int i=0;i<prodExportList.size();i++) {
					ProdExport model = prodExportList.get(i);
					sheet.addCell(new Label(0, i+1, model.getInfoCategory(), contentCenterFormat));
					sheet.addCell(new Label(1, i+1, model.getProdName(), contentLeftFormat));
					sheet.addCell(new Label(2, i+1, model.getProdType(), contentCenterFormat));
					sheet.addCell(new Label(3, i+1, model.getPrice().toString(), contentRightFormat));
					sheet.addCell(new Label(4, i+1, model.getBrand(), contentLeftFormat));
					sheet.addCell(new Label(5, i+1, model.getProdLevel(), contentCenterFormat));
					sheet.addCell(new Label(6, i+1, model.getHotFlag(), contentCenterFormat));
					sheet.addCell(new Label(7, i+1, model.getProdOwner(), contentLeftFormat));
					sheet.addCell(new Label(8, i+1, model.getAmount(), contentLeftFormat));
					sheet.addCell(new Label(9, i+1, model.getUnit(), contentCenterFormat));
					sheet.addCell(new Label(10, i+1, model.getPublishTime(), contentCenterFormat));
					sheet.addCell(new Label(11, i+1, model.getEndDate().toString(), contentCenterFormat));
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}
		return book;
	}

}
